<template>
	<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk"
	 @cancel="handleCancel" cancelText="关闭">
		<a-spin :spinning="confirmLoading">
			<a-form :form="form">
				<a-form-item label="生产单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<!-- <a-input v-decorator="['produceCode']" placeholder="请输入生产单号"></a-input> -->
					<j-popup v-decorator="['produceCode']" :trigger-change="true" org-fields="materiel_id,materiel_code,materiel_name,gauge,gross_account,plan_start,plan_end,id,order_code"
					 dest-fields="materielId,mechanismCode,mechanismName,gague,plantNum,expectBegintime,expectFinishtime,produceId,produceCode"
					 code="produceorder_search" @callback="popupCallback" />
				</a-form-item>
        <a-form-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['workbillCode',validatorRules.workbillCode]" placeholder="请输入工单号"></a-input>
        </a-form-item>
				<a-form-item label="机种料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['mechanismCode']" placeholder="请输入生产单号"></a-input>
				</a-form-item>
				<a-form-item label="机种名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['mechanismName']" placeholder="请输入机种名称"></a-input>
				</a-form-item>
				<a-form-item label="计划数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['plantNum']" placeholder="请输入计划数量"></a-input>
				</a-form-item>
				<a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['gague']" placeholder="请输入规格"></a-input>
				</a-form-item>
				<!-- <a-form-item label="线别" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-popup v-decorator="['lineType']" :trigger-change="true" org-fields="id,productline_name" dest-fields="lineId,lineType"
					 code="productline_search" @callback="popupCallback" />
				</a-form-item>
				<a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['client']" placeholder="请输入客户"></a-input>
				</a-form-item> -->
				<a-form-item label="机种版本号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['mechanismEdition']" placeholder="请输入机种版本号"></a-input>
				</a-form-item>
				<a-form-item label="工单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['workbillType']" placeholder="请输入工单类型"></a-input>
				</a-form-item>
				<a-form-item label="生产阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-dict-select-tag type="list" v-decorator="['produceGrade', validatorRules.produceGrade]" :trigger-change="true"
					 dictCode="produce_grade" placeholder="请选择生产阶别" />
				</a-form-item>
				<a-form-item label="自动下达制令单" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-dict-select-tag type="radio" v-decorator="['automaticCommandbill']" :trigger-change="true" dictCode="yn"
					 placeholder="请选择自动下达制令单" />
				</a-form-item>
				<a-form-item label="首件检测数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['firstChecknum']" placeholder="请输入首件检测数量"></a-input>
				</a-form-item>
				<a-form-item label="预计开工时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择预计开工时间" v-decorator="['expectBegintime']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="实际开工时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择实际开工时间" v-decorator="['realBegintime']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="计划交货时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择计划交货时间" v-decorator="['plantDelivertime']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="预计完工时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择预计完工时间" v-decorator="['expectFinishtime']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="实际完工时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择实际完工时间" v-decorator="['realFinishtime']" :trigger-change="true" :show-time="true"
					 date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
				</a-form-item>
				<a-form-item label="包装顺序管控" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-dict-select-tag type="radio" v-decorator="['parcelSequence']" :trigger-change="true" dictCode="yn" placeholder="请选择包装顺序管控" />
				</a-form-item>
				<a-form-item label="号段管控" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-dict-select-tag type="radio" v-decorator="['paraControl']" :trigger-change="true" dictCode="yn" placeholder="请选择号段管控" />
				</a-form-item>
				<a-form-item label="号段条码规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['paraBarcode']" placeholder="请输入号段条码规则"></a-input>
				</a-form-item>
				<a-form-item label="固定字符" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['fixedCharacter']" placeholder="请输入固定字符"></a-input>
				</a-form-item>
				<a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注" />
				</a-form-item>
				<a-form-item v-show="false" label="产线ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-show="false" v-decorator="['lineId']" placeholder="请输入产线ID"></a-input>
				</a-form-item>
				<a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-show="false" v-decorator="['materielId']" placeholder="请输入物料ID"></a-input>
				</a-form-item>
				<a-form-item v-show="false" label="生产订单ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-show="false" v-decorator="['produceId']" placeholder="请输入生产订单ID"></a-input>
				</a-form-item>
			</a-form>
		</a-spin>
	</j-modal>
</template>

<script>
	import {
		httpAction
	} from '@/api/manage'
	import pick from 'lodash.pick'
	import {
		validateDuplicateValue
	} from '@/utils/util'
	import JDate from '@/components/jeecg/JDate'
	import JDictSelectTag from "@/components/dict/JDictSelectTag"


	export default {
		name: "MesWorkbillInfoModal",
		components: {
			JDate,
			JDictSelectTag,
		},
		data() {
			return {
				form: this.$form.createForm(this),
				title: "操作",
				width: 800,
				visible: false,
				model: {},
				labelCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 5
					},
				},
				wrapperCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 16
					},
				},
				confirmLoading: false,
				validatorRules: {
          workbillCode: {
          	rules: [{
          		required: true,
          		message: '请输入工单号!'
          	}, ]
          }
        },
				url: {
					add: "/produce/mesWorkbillInfo/add",
					edit: "/produce/mesWorkbillInfo/edit",
				}
			}
		},
		created() {},
		methods: {
			add() {
				this.edit({});
			},
			edit(record) {
				this.form.resetFields();
				this.model = Object.assign({}, record);
				this.visible = true;
				this.$nextTick(() => {
					this.form.setFieldsValue(pick(this.model, 'createBy', 'createTime', 'workbillCode', 'lineType', 'client',
						'mechanismCode', 'mechanismName', 'gague', 'mechanismEdition', 'workbillType', 'produceGrade', 'plantNum',
						'automaticCommandbill', 'firstChecknum', 'expectBegintime', 'realBegintime', 'plantDelivertime',
						'expectFinishtime', 'realFinishtime', 'produceCode', 'parcelSequence', 'paraControl', 'paraBarcode',
						'fixedCharacter', 'notes', 'lineId', 'materielId', 'produceId'))
				})
			},
			close() {
				this.$emit('close');
				this.visible = false;
			},
			handleOk() {
				const that = this;
				// 触发表单验证
				this.form.validateFields((err, values) => {
					if (!err) {
						that.confirmLoading = true;
						let httpurl = '';
						let method = '';
						if (!this.model.id) {
							httpurl += this.url.add;
							method = 'post';
						} else {
							httpurl += this.url.edit;
							method = 'put';
						}
						let formData = Object.assign(this.model, values);
						console.log("表单提交数据", formData)
						httpAction(httpurl, formData, method).then((res) => {
							if (res.success) {
								that.$message.success(res.message);
								that.$emit('ok');
							} else {
								that.$message.warning(res.message);
							}
						}).finally(() => {
							that.confirmLoading = false;
							that.close();
						})
					}

				})
			},
			handleCancel() {
				this.close()
			},
			popupCallback(row) {
				this.form.setFieldsValue(pick(row, 'createBy', 'createTime', 'workbillCode', 'lineType', 'client', 'mechanismCode',
					'mechanismName', 'gague', 'mechanismEdition', 'workbillType', 'produceGrade', 'plantNum', 'automaticCommandbill',
					'firstChecknum', 'expectBegintime', 'realBegintime', 'plantDelivertime', 'expectFinishtime', 'realFinishtime',
					'produceCode', 'parcelSequence', 'paraControl', 'paraBarcode', 'fixedCharacter', 'notes', 'lineId', 'materielId',
					'produceId'))
			},


		}
	}
</script>
